package view;

import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

public class ProductActionForm extends ActionForm {
	private String id = null;
	private String name = null;
	private String price = null;
	private String make = null;
	private String expire = null;
	private String prodaction = null;
	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd");

	@Override
	public ActionErrors validate(ActionMapping mapping,
			HttpServletRequest request) {
		ActionErrors errors = new ActionErrors();
		
		if (id!= null && id.length()!=0){
			try {
				Integer.parseInt(id);
			} catch (NumberFormatException e) {
				errors.add("id", new ActionMessage("product.transformerror.id"));
				e.printStackTrace();
			}
		}	
		if(make!=null&& make.length()!=0){
			try {
				sdf.parse(make);
			} catch (ParseException e) {
				errors.add("make", new ActionMessage("product.transformerror.make"));
				e.printStackTrace();
			}
			
		}
		if(expire!=null&& expire.length()!=0){
			try {
				Integer.parseInt(expire);
			} catch (NumberFormatException e) {
				errors.add("expire", new ActionMessage("product.transformerror.expire"));
				e.printStackTrace();
			}
			
		}
		if(price!=null&& price.length()!=0){
			try {
				Double.parseDouble(price);
			} catch (NumberFormatException e) {
				errors.add("price", new ActionMessage("product.transformerror.price"));
				e.printStackTrace();
			}
		}
		//Validate 
		if(prodaction==null || 
				!(prodaction.equals("Insert")||prodaction.equals("Update")||prodaction.equals("Delete")|| prodaction.equals("Select"))){
			
			errors.add("prodaction", new ActionMessage("prodaction.unknownerror"));
		}
		if(prodaction!=null &&
				(prodaction.equals("Insert")||prodaction.equals("Update")||prodaction.equals("Delete"))){
			if(id==null||id.length()==0){
				errors.add("id", new ActionMessage("product.null.id",prodaction));
			}
		}
		
		
		return errors;
	}

	public String getProdaction() {
		return prodaction;
	}



	public void setProdaction(String prodaction) {
		this.prodaction = prodaction;
	}



	public String getId() {
		return id;
	}



	public void setId(String id) {
		if(id!=null && id.length()!=0){
			this.id = id;
		}
		
	}



	public String getName() {
		return name;
	}



	public void setName(String name) {
		if(name!=null && name.length()!=0){
			this.name = name;
		}
		
	}



	public String getPrice() {
		return price;
	}



	public void setPrice(String price) {
		
		if(price!=null && price.length()!=0){
			this.price = price;
		}
	}



	public String getMake() {
		return make;
	}



	public void setMake(String make) {
		if(make!=null && make.length()!=0){
			this.make = make;
		}
		
	}



	public String getExpire() {
		return expire;
	}



	public void setExpire(String expire) {
		if(expire!=null && expire.length()!=0){
			this.expire = expire;
		}
		
	}



	

}
